<template>
	<view>
		<view class="item" v-for="(res, index) in siteList" :key="res.id">
			<view class="top" @click="choose(res)">
				<view class="name">{{ res.name }}</view>
				<view class="phone">{{ res.phone }}</view>
				<view class="tag" v-if="res.is_default=='1'">
					<!-- <text v-for="(item, index) in res.tag" :key="index" :class="{red:item.is_fault=='1'}">{{ item.tagText }}</text> -->
					<text class="red">默认</text>
				</view>
			</view>
			<view class="bottom">
				{{res.province+res.city+res.county+'&#32&#32&#32&#32'+res.address}}
				<u-icon name="edit-pen" :size="40" color="#999999" @click="gotoEdit(res.id)" label="编辑"></u-icon>
				<u-icon name="man-delete" :size="40" color="#999999" @click="manDelete(res.id)" label="删除"></u-icon>
			</view>
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				siteList: []
			};
		},
		async onLoad() {
			this.getData();
		},
		methods: {
			async getData() {
				const res = await this.$u.api.addressList();
				//console.log('res', res)
				this.siteList = res.data
			},
			toAddSite() {
				uni.navigateTo({
					url: '/pages/cart/address_list/addSite'
				});
			},
			gotoEdit(id) {
				uni.navigateTo({
					url: '/pages/cart/address_list/addSite?id=' + id
				});
			},
			async manDelete(id) {
				uni.showModal({
					title: '提示',
					content: '确认删除吗？',
					success: (res)=> {
						if (res.confirm) {
							console.log('用户点击确定');
							this.$u.api.addressDelete(id)
							uni.showToast({
								icon: 'success',
								title: '操作成功'
							})
							//刷新页面地址数据
							this.getData();
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//选区收货地址
			choose(res){
				console.log('choose',res)
				uni.navigateTo({
					url: '/pages/cart/create?addressId='+res.id
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.item {
		padding: 40rpx 20rpx;

		.top {
			display: flex;
			font-weight: bold;
			font-size: 34rpx;

			.phone {
				margin-left: 60rpx;
			}

			.tag {
				display: flex;
				font-weight: normal;
				align-items: center;

				text {
					display: block;
					width: 60rpx;
					height: 34rpx;
					line-height: 34rpx;
					color: #ffffff;
					font-size: 20rpx;
					border-radius: 6rpx;
					text-align: center;
					margin-left: 30rpx;
					background-color: rgb(49, 145, 253);
				}

				.red {
					background-color: red
				}
			}
		}

		.bottom {
			display: flex;
			margin-top: 20rpx;
			font-size: 28rpx;
			justify-content: space-between;
			color: #999999;
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 600rpx;
		line-height: 100rpx;
		position: absolute;
		bottom: 30rpx;
		left: 80rpx;
		background-color: red;
		border-radius: 60rpx;
		font-size: 30rpx;

		.add {
			display: flex;
			align-items: center;
			color: #ffffff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>
